import 'package:fluent_ui/fluent_ui.dart' hide IconButton, Colors;
import 'package:flutter/material.dart';
import 'package:fnos_tv_desktop/application/nav.dart';
import 'package:provider/provider.dart';

import '../../../application/application.dart';
import '../../../core/model/user_info.dart';

class TitleBarActions extends StatefulWidget {
  const TitleBarActions({super.key});

  @override
  State<TitleBarActions> createState() => _TitleBarActionsState();
}

class _TitleBarActionsState extends State<TitleBarActions> {
  final flyoutController = FlyoutController();

  @override
  void dispose() {
    flyoutController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final user = context.watch<UserInfo>();
    return Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        IconButton(icon: const Icon(Icons.search), onPressed: () {}),
        FlyoutTarget(
          controller: flyoutController,
          child: IconButton(
            icon: const Icon(Icons.person),
            onPressed: () {
              flyoutController.showFlyout(
                autoModeConfiguration: FlyoutAutoConfiguration(
                  preferredMode: FlyoutPlacementMode.bottomCenter,
                ),
                barrierDismissible: true,
                barrierColor: Colors.transparent,
                dismissOnPointerMoveAway: false,
                navigatorKey: Application.navState,
                builder: (context) {
                  return Container(
                    padding: EdgeInsets.all(18),
                    child: MenuFlyout(
                      constraints: BoxConstraints(minWidth: 240),
                      itemMargin: EdgeInsetsDirectional.symmetric(
                        horizontal: 14.0,
                        vertical: 4.0,
                      ),
                      items: [
                        MenuFlyoutItemBuilder(
                          builder: (context) {
                            return _UserInfoItem();
                          },
                        ),
                        MenuFlyoutItem(
                          leading: Icon(Icons.lock_outline),
                          text: Text('修改密码'),
                          onPressed: () {},
                        ),
                        MenuFlyoutItem(
                          leading: Icon(Icons.play_circle_outline),
                          text: Text('播放偏好设置'),
                          onPressed: () {},
                        ),
                        MenuFlyoutItem(
                          leading: Icon(Icons.color_lens_outlined),
                          text: Text('外观'),
                          onPressed: () {},
                        ),
                        MenuFlyoutSeparator(),
                        MenuFlyoutItem(
                          leading: Icon(Icons.help_outline),
                          text: Text('帮助中心'),
                          onPressed: () {},
                        ),
                        MenuFlyoutItem(
                          leading: Icon(Icons.info_outline),
                          text: Text('关于'),
                          onPressed: () {},
                        ),
                        MenuFlyoutSeparator(),
                        MenuFlyoutItem(
                          leading: Icon(Icons.exit_to_app),
                          text: Text('退出'),
                          onPressed: () {
                            Nav.logout();
                          },
                        ),
                      ],
                    ),
                  );
                },
              );
            },
          ),
        ),
        if (user.isAdmin)
          IconButton(icon: const Icon(FluentIcons.settings), onPressed: () {}),
      ],
    );
  }
}

class _UserInfoItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final user = context.watch<UserInfo>();
    return Row(
      children: [
        CircleAvatar(
          child: Text(
            (user.username?.isNotEmpty == true ? user.username! : '?')
                .substring(0, 1)
                .toUpperCase(),
          ),
        ),
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text(
                    user.username ?? '未登录',
                    style: FluentTheme.of(context).typography.body,
                  ),
                  if (user.isAdmin)
                    Container(
                      margin: const EdgeInsets.only(left: 8),
                      padding: const EdgeInsets.symmetric(
                        horizontal: 8,
                        vertical: 2,
                      ),
                      decoration: BoxDecoration(
                        color: Colors.blue[900]!.withAlpha(100),
                        borderRadius: BorderRadius.circular(100),
                      ),
                      child: Text(
                        '管理员',
                        style: TextStyle(
                          fontSize: 12,
                          color: FluentTheme.of(context).accentColor,
                        ),
                      ),
                    ),
                ],
              ),
              const SizedBox(height: 4),
              Text(
                '飞牛影视',
                style: FluentTheme.of(
                  context,
                ).typography.caption?.copyWith(color: Colors.white70),
              ),
            ],
          ),
        ),
      ],
    );
  }
}
